Destroying the Resources

Learn how to destroy all the resources that we created on the Google Cloud platform.

We'll cover the following

Deleting the resources#

We’re (almost) finished with the quick exploration of Terraform using GKE as an example. We’ve seen how to add and change resources, and the only thing missing is to learn how to destroy them.

If we want to delete some of the resources, all we have to do is remove their definitions and execute terraform apply. However, in some cases, we might want to destroy everything. There’s a command for that as well.

We need to keep the storage where the Terraform state is stored. That will allow us to re-create the same cluster for the rest of the exercises. If we use AWS or GCP, we simply use the terraform destroy command because those two don’t allow us to destroy storage if there are files inside it. Or, to be more precise, that’s the default behavior, and we need to specify explicitly that we do want to destroy storage even if it contains files by setting the argument force_destroy to true. However, Azure doesn’t have such a flag. So, if we execute terraform destroy, everything will be gone, including the storage with Terraform state. Since we want to keep that storage, we need to tell Terraform which targets to destroy instead of wiping out everything.

In this case, we do want to destroy all the resources except the storage with Terraform state. We can do that through the --target argument. Fortunately for us, AKS is simple, and the whole cluster is defined as a single resource called azurerm_kubernetes_cluster.

Command to destroy resources

At the end of the process, we might see an error stating that it couldn’t delete the bucket with Terraform state without force_destroy set to true. Don’t be alarmed. That’s normal. After Terraform destroys everything, it tries to destroy the bucket where we keep the state. However, we didn’t specify that the bucket can be removed if it contains files. The process failed to remove that bucket, and that’s a good thing. That allows us to re-create the same cluster in the sections that follow.

The cluster and all the other resources we defined are now gone. The exception is the storage with the state that we left intact and that we’ll continue using in the exercises that follow.

Prepare to destroy

Please note that we only removed the resources created through Terraform, excluding the bucket. Those that were created with gcloud (e.g., project, service account, etc.) are still there. Google won’t charge us anything (or much) for them, so, unlike those we created with Terraform, there is no good reason to remove them. On the other hand, we might want to use the definitions from this chapter to create a cluster that will be used for the exercises in the others. Keeping those created with gcloud will simplify the process. All we’d have to do is execute terraform apply.

Try it yourself#

You can try all of the commands used in this lesson in the code playground below. Press the “Run” button and wait for a few seconds for it to connect.

For ease of use, all of the commands above are combined in main.sh.

Please provide values for the following:
type
Not Specified...
project_id
Not Specified...
private_key_id
Not Specified...
private_key
Not Specified...
client_email
Not Specified...
client_id
Not Specified...
auth_uri
Not Specified...
token_uri
Not Specified...
auth_provider_x509_cert_url
Not Specified...
client_x509_cert_url
Not Specified...
GCP_TF_VAR_state_bucket
Not Specified...
GCP_PROJECT_ID
Not Specified...
TF_VAR_project_id
Not Specified...
/
account.json
main.sh
backend.tf
k8s-control-plane.tf
k8s-worker-nodes.tf
output.tf
provider.tf
storage.tf
variables.tf
Try it yourself

Reorganizing Definitions

Summary: GKE